#include "ImageFilterList.h"

ImageFilterList::ImageFilterList(void) : nmbFilters(0), mFilters(NULL)
{
}

ImageFilterList::~ImageFilterList(void)
{
	delete [] mFilters;
}

bool ImageFilterList::apply(Image& pImage)
{
	if(mFilters)
	{
		for(unsigned int i = 0; i < nmbFilters ; i++)
		{
			bool success = mFilters[i]->apply(pImage);

			if(!success)
			{
				return false;
			}
		}
	}
	return true;
}
void ImageFilterList::addImageFilter(ImageFilter* pFilter)
{
	if(pFilter != NULL)
	{
		ImageFilter** aux = new ImageFilter*[nmbFilters + 1];

		if(mFilters != NULL )
		{
			for(unsigned int i = 0; i < nmbFilters; i++)
			{
				aux[i] = mFilters[i];
			}
			delete [] mFilters;
		}
		aux[nmbFilters] = pFilter;

		nmbFilters++;

		mFilters = aux;
	}

}
void ImageFilterList::remove(ImageFilter* pFilter)
{
	bool found = false;
	int ptrPos = 0;
	
	if(pFilter == NULL)
	{
		return;
	}

	for(unsigned int i = 0; i < nmbFilters && !found; i++)
	{
		if(mFilters[i] == pFilter)
		{
			ptrPos = i;
			found = true;
		}
	}

	if(found)
	{
		ImageFilter** aux = new ImageFilter*[nmbFilters - 1];

		
		std::copy(mFilters, mFilters+ptrPos, aux);
	
		std::copy(mFilters+(ptrPos+1), mFilters+nmbFilters, aux+ptrPos);
		
		delete [] mFilters;
		mFilters = aux;
		nmbFilters--;
	}
}